I’m new to C++ and unsure about how to improve this code [migrated]

Posted by Laian Alsabbagh on Programmers See other posts from Programmers or by Laian Alsabbagh
Published on 2012-11-03T17:31:14Z Indexed on 2012/11/03 23:17 UTC
Read the original article Hit count: 162

Filed under:
|

The purpose of the following code is to get a random number of 100 nodes and to distribute these nodes randomly in range 500*500 …(X,Y).. this was the first step

#include<iostream>  
#include <fstream>  
#include<cmath>  

using namespace std;  

int  main()   
{  
 const int x = 0, y = 1;   
 int nodes[100][2];    
 ofstream myfile;  
 myfile.open ("example.txt");     
 myfile << "Writing this to a file.\n";  

 for (int i=0; i<100 ;i++)  
 {     
      nodes[i][x] = rand() % 501;    
      nodes[i][y] = rand() % 501;  
      myfile <<nodes[i][x]<<" "<<nodes[i][y];  
 }

 myfile.close();
}

now the next step is to improve this code to distribute these nodes in order ( "Imust divide both xy_coordinates as : x= 0-100-200-300-400-500 & y=0-100-200-300-400-500) next is to distribute the nodes (regardless number of nodes) in order range Starting from (0,100 )….(100,100)..(100,200)…….untile i reach the last point (500,500),, ")

I’m really confused of how to do it correctly I start to think to define 2 dimensional array , and then to define 2 for loops

    enter code here
     Int no_nodes=100;  
    Int  XY_coordinate [500][500];  
     For (int i=0;i<no_nodes; i++)  
  {  
 For (int j=0;j<no_nodes; j++) 

© Programmers or respective owner

Related posts about c++

Related posts about c